//
//  ViewController.swift
//  XHWalletUtils
//
//  Created by xinph on 2020/5/12.
//  Copyright © 2020 xinph. All rights reserved.
//

import UIKit


class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
//        guard let wallets = Manager.IO.readAllWallets(Wallet.self) else {
//            return
//        }
        
        
        
        
//        print(wallets.first?.keystore)
        
        
//        return
        
        
        let password = ""
//        let mnemonics = "license blouse scorpion nuclear visit ceiling find consider confirm scrap guilt power"
        let mnemonics = "easily across shrimp trade calm valve list mobile resist rose armor fragile"
        
            
        ///构建btc钱包
        co_launch {
            _ = try await(promise: BTC.importAccountByMnemonics("123456", name: "", mnemonics: mnemonics, saveToLocal: false))
        }

        /// 构建eth钱包
        co_launch {
            let rst = try await(promise: ETH.importAccountByMnemonics(password, name: "ETH", mnemonics: mnemonics, saveToLocal: false))
            
            //// 交易对象
            struct XXXX: TransactionConvertible {
                var proposalId: Int
                
                var nonce: Float?
                
                var gasPrice: Float
                
                var gasLimit: Float
                
                var to: AddressConvertible
                
                var value: Float
                
                var data: Data?
                
                var chainID: Int?
                
                static func creat() -> XXXX {
                    let x = XXXX(proposalId: 10002, gasPrice: 4100000000, gasLimit: 9000000, to: "0xA63931c0ABDF688407eBf86DeAa83787464943ED", value: 0)
                    return x
                }
            }
            
            switch rst {
            case .fulfilled(let wallet):
                let address = wallet.accounts.first?.address
                print(address)
                
//                guard let account = address else { return }
//                let `private` = ETH.UNSAFE_getPrivateAndPublicKey(password: password, address: account)
//                print("私钥：\(`private`)")
                
    //            let ts = XXXX()
    //
    //            let from = wallet.accounts.first
    //            let wallet = wallet as! ETH
    //            wallet.sendTransactionUnSigned(ts, password: "", from: "")
    //                .subscribe {
    //                    print($0)
    //            }
                
    //            wallet.signTX(ts, password: password, from: wallet.accounts.first!)
                

            default:
                print("导入失败")
            }

        }
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        return
        
//        let mnemonics = "excess crush this neglect excess tobacco width wood scale wrap slush federal"
        
        
        Manager.shared.password = password
        let finalWallet: Wallet?

        guard let eth = Manager.IO.updatedWallet(Wallet.self, obj: finalWallet!) else {
            //引入账户
//            let wallet = ETH.importAccountByMnemonics(password, mnemonics: mnemonics) as! ETH
//            wallet.createSevralAccounts(password, need: 13, cache: true)
//            print("引入账户成功")
            return
        }

        if eth.vertifyKeystore(password) {
            print("离线验证keystore成功")
        } else {
            print("离线验证keystore失败")
        }
        
        ///获取私钥
        guard let account = eth.accounts.first else { return }
        let `private` = ETH.UNSAFE_getPrivateAndPublicKey(password: password, address: account.address)
        print("私钥：\(`private`)")
        
        
            
//
//
//       //获取余额
//        eth.getETHBalance(eth.accounts.first!.address) {
//            switch $0 {
////            case .success(let balance): print("余额：\(balance)")
//            default: break
//            }
//        }
//
//        eth.getAccounts {
//            switch $0 {
////            case .success(let account): print("账户：\(account)")
//            default: break
//            }
//        }
//
//        //自定义合约转账
//        eth.writeContract(eth.accounts.first!.address, to: eth.accounts.first!.address, money: "0.0012", password: "12345678") {
//            if case Result.success(let rst) = $0 {
//                eth.getTransactionDetails(rst.hash) {
//                    if case Result.success(let detail) = $0 {
//                        print(detail)
//                    }
//                }
//            }
//        }
//
    }

}
